<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.cmq.web_1.cart.Product" %>
<html>
<head>
    <title>我的购物车</title>
    <style>
        .container { max-width: 1000px; margin: 0 auto; padding: 20px; }
        .message { padding: 10px; background: #d4edda; color: #155724; text-align: center; margin-bottom: 20px; }
        .back-link { display: inline-block; padding: 8px 15px; background: #6c757d; color: white; text-decoration: none; border-radius: 5px; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        th, td { padding: 12px; border-bottom: 1px solid #ddd; text-align: left; }
        .quantity-btn { width: 30px; height: 30px; }
        .remove-btn { background: #dc3545; color: white; border: none; padding: 5px 10px; border-radius: 3px; cursor: pointer; }
        .summary { text-align: right; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .clear-btn { background: #ffc107; border: none; padding: 8px 15px; border-radius: 3px; cursor: pointer; margin-top: 10px; }
    </style>
</head>
<body>
<div class="container">
    <h1>我的购物车</h1>

    <%-- 显示提示信息 --%>
    <%
        String message = (String) session.getAttribute("message");
        if (message != null) {
    %>
    <div class="message"><%= message %></div>
    <% session.removeAttribute("message"); } %>

    <%-- 返回商品列表 --%>
    <a href="${pageContext.request.contextPath}/cart/products.jsp" class="back-link">返回商品列表</a>

    <%-- 购物车数据 --%>
    <%
        Map<String, Integer> cart = (Map<String, Integer>) request.getAttribute("cart");
        Map<String, Product> products = (Map<String, Product>) request.getAttribute("products");
        Double totalPrice = (Double) request.getAttribute("totalPrice");
        Integer totalQuantity = (Integer) request.getAttribute("totalQuantity");

        // 判空处理，避免null报错
        if (cart == null) cart = new java.util.HashMap<>();
        if (products == null) products = new java.util.HashMap<>();
        if (totalPrice == null) totalPrice = 0.0;
        if (totalQuantity == null) totalQuantity = 0;
    %>

    <% if (cart.isEmpty()) { %>
    <div style="text-align:center; padding:50px; border:1px solid #ddd;">
        <h2>购物车是空的</h2>
        <a href="${pageContext.request.contextPath}/cart/products.jsp">去添加商品</a>
    </div>
    <% } else { %>
    <table>
        <tr>
            <th>商品名称</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </tr>
        <% for (Map.Entry<String, Integer> entry : cart.entrySet()) {
            String pid = entry.getKey();
            int qty = entry.getValue();
            Product p = products.get(pid);
            if (p != null) {
        %>
        <tr>
            <td><%= p.getName() %></td>
            <td>¥<%= String.format("%.2f", p.getPrice()) %></td>
            <td>
                <form action="${pageContext.request.contextPath}/cart" method="post" style="display:inline;">
                    <input type="hidden" name="action" value="decrease">
                    <input type="hidden" name="productId" value="<%= pid %>">
                    <button type="submit" class="quantity-btn">-</button>
                </form>
                <span><%= qty %></span>
                <form action="${pageContext.request.contextPath}/cart" method="post" style="display:inline;">
                    <input type="hidden" name="action" value="add">
                    <input type="hidden" name="productId" value="<%= pid %>">
                    <button type="submit" class="quantity-btn">+</button>
                </form>
            </td>
            <td>¥<%= String.format("%.2f", p.getPrice() * qty) %></td>
            <td>
                <form action="${pageContext.request.contextPath}/cart" method="post" onsubmit="return confirm('确定删除？')">
                    <input type="hidden" name="action" value="remove">
                    <input type="hidden" name="productId" value="<%= pid %>">
                    <button type="submit" class="remove-btn">删除</button>
                </form>
            </td>
        </tr>
        <% } } %>
    </table>

    <div class="summary">
        <div>商品总数：<%= totalQuantity %> 件</div>
        <div style="font-size:20px; color:red; font-weight:bold;">
            总计：¥<%= String.format("%.2f", totalPrice) %>
        </div>
        <form action="${pageContext.request.contextPath}/cart" method="post" onsubmit="return confirm('确定清空？')">
            <input type="hidden" name="action" value="clear">
            <button type="submit" class="clear-btn">清空购物车</button>
        </form>
    </div>
    <% } %>
</div>
</body>
</html>